var MongoClient = require('mongodb').MongoClient;
var Dbs = /** @class */ (function () {
    function Dbs() {
        this.MongoClient = MongoClient;
        this.url = 'mongodb://localhost:27017/';
    }
    Dbs.prototype.CreateCollection = function (name) {
        MongoClient.connect(this.url, function (err, db) {
            if (err)
                throw err;
            console.log('数据库已创建');
            var dbase = db.db("mongo");
            dbase.createCollection(name, function (err, res) {
                if (err)
                    throw err;
                console.log("创建集合!");
                db.close();
            });
        });
    };
    Dbs.prototype.deleteMany = function (collection, query) {
        MongoClient.connect(this.url, function (err, db) {
            if (err)
                throw err;
            var dbo = db.db("mongo");
            var whereStr = query; // 查询条件
            dbo.collection(collection).deleteMany(whereStr, function (err, obj) {
                if (err)
                    throw err;
                console.log(obj.result.n + " 条文档被删除");
                db.close();
            });
        });
    };
    Dbs.prototype.deleteOne = function (collection, query) {
        MongoClient.connect(this.url, function (err, db) {
            if (err)
                throw err;
            var dbo = db.db("mongo");
            var whereStr = query; // 查询条件
            dbo.collection(collection).deleteOne(whereStr, function (err, obj) {
                if (err)
                    throw err;
                console.log("文档删除成功");
                db.close();
            });
        });
    };
    Dbs.prototype.find = function (collection, query, callback) {
        MongoClient.connect(this.url, { useNewUrlParser: true }, function (err, db) {
            if (err)
                throw err;
            var dbo = db.db("mongo");
            var where = query;
            dbo.collection(collection).find(where).toArray(function (err, result) {
                if (err)
                    throw err;
                callback(result);
                db.close();
            });
        });
    };
    Dbs.prototype.insert = function (collection, query) {
        MongoClient.connect(this.url, function (err, db) {
            if (err)
                throw err;
            var dbo = db.db("mongo");
            var myobj = query;
            dbo.collection(collection).insertOne(myobj, function (err, res) {
                if (err)
                    throw err;
                console.log("文档插入成功");
                db.close();
            });
        });
    };
    Dbs.prototype.insertMany = function (collection, query) {
        MongoClient.connect(this.url, function (err, db) {
            if (err)
                throw err;
            var dbo = db.db("mongo");
            var myobj = query;
            dbo.collection(collection).insertMany(myobj, function (err, res) {
                if (err)
                    throw err;
                console.log("插入的文档数量为: " + res.insertedCount);
                db.close();
            });
        });
    };
    Dbs.prototype.SortCollection = function (collection, query) {
        MongoClient.connect(this.url, function (err, db) {
            if (err)
                throw err;
            var dbo = db.db("mongo");
            var mysort = query;
            dbo.collection(collection).find().sort(mysort).toArray(function (err, result) {
                if (err)
                    throw err;
                console.log(result);
                db.close();
            });
        });
    };
    Dbs.prototype.updateMany = function (collection, query, content) {
        MongoClient.connect(this.url, function (err, db) {
            if (err)
                throw err;
            var dbo = db.db("mongo"); //collections
            var whereStr = query; // 查询条件
            var updateStr = { $set: content }; //更新的地方
            dbo.collection(collection).updateMany(whereStr, updateStr, function (err, res) {
                if (err)
                    throw err;
                console.log(res.result.nModified + " 条文档被更新");
                db.close();
            });
        });
    };
    Dbs.prototype.updateOne = function (col, que, content) {
        MongoClient.connect(this.url, function (err, db) {
            if (err)
                throw err;
            var dbo = db.db("mongo");
            var whereStr = que; // 查询条件
            var updateStr = { $set: content };
            dbo.collection(col).updateOne(whereStr, updateStr, function (err, res) {
                if (err)
                    throw err;
                console.log("文档更新成功");
                db.close();
            });
        });
    };
    return Dbs;
}());

module.exports = Dbs